<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-menu"></i>{{$route.name}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div>
            <div class="handle-box">
                <el-button type="success" icon="search" @click="addType">添加类别</el-button>
            </div>
            <div>
                <el-dialog :title="title" :visible.sync="dialogFormVisible">
                    <el-form :model="addForm">
                        <el-form-item label="栏目名称" :label-width="formLabelWidth">
                            <el-input v-model="addForm.name" placeholder="请填写栏目名称" auto-complete="off"></el-input>
                        </el-form-item>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="cancel">取 消</el-button>
                        <el-button type="primary" @click="add">确 定</el-button>
                    </div>
                </el-dialog>
            </div>
            <div>
                <el-table :data="bookType" border fit>
                    <el-table-column prop="name" label="书籍类别" align="center">
                    </el-table-column>
                    <el-table-column fixed="right" label="操作" align="center">
                        <template slot-scope="scope">
                            <el-button @click="put(scope.row)">编辑</el-button>
                            <el-button @click="del(scope.row.id)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </div>
    </div>
</template>
<script>
import { bookTypes, editBookType, delBookType } from '../../api/server.js'
import { mapActions } from 'vuex'
export default {
    data() {
        return {
            bookType: [],
            title: '',
            dialogFormVisible: false,
            addForm: {
                name: ''
            },
            formLabelWidth: '120px'
        }
    },
    created() {
        this.getAllBookType(1, 10)
    },
    methods: {
        ...mapActions(['saveBookTypes']),
        addType() {
            this.addForm = {}
            this.dialogFormVisible = true
            this.title = '添加分类'
        },
        add() {
            let booktype = this.addForm
            editBookType(booktype)
                .then(res => {
                    this.dialogFormVisible = false
                    this.$message.info('编辑成功')
                    this.getAllBookType(1, 10)
                })
        },
        getAllBookType(index, page) {
            bookTypes({ index, page })
                .then(res => {
                    this.bookType = res.result
                })
        },
        put(row) {
            this.addForm = row
            this.dialogFormVisible = true
            this.title = '修改分类'
        },
        cancel() {
            this.getAllBookType(1, 10)
            this.dialogFormVisible = false
        },
        del(id) {
            delBookType({ id })
                .then(res => {
                    this.$message.success('删除成功')
                    this.getAllBookType(1, 10)
                })
        }
    }
}

</script>
<style lang="scss" scoped>
.handle-box {
  margin-bottom: 1%;
  .search {
    width: 30%;
  }
}

.block {
  text-align: center;
  margin-top: 1%;
}
</style>